package me.ak.gui;

import de.matthiasmann.twl.Button;
import de.matthiasmann.twl.DialogLayout;
import de.matthiasmann.twl.Label;
import de.matthiasmann.twl.Widget;

public class Alert {
	
	public static boolean stopInteract = false;

	public static void displayMessage(String message) {
		if (stopInteract) return;
		MessageWidget mw = new MessageWidget();
		mw.setMessage(message);
		MainCanvas.root.add(mw.getWidget());
		stopInteract = true;
	}

	private static class MessageWidget implements WidgetAdapter {
		public Dialog panel;
		public Label message;
		public Button okb;

		public MessageWidget() {
			panel = new Dialog();
			panel.setTheme("panel");
			okb = new Button("OK");
			okb.addCallback(new Runnable() {
				public void run() {
					MainCanvas.root.removeChild(MessageWidget.this.getWidget());
					MessageWidget.this.getWidget().destroy();
					Alert.stopInteract = false;
				}
			});
			message = new Label("");

			reAdjust();
		}

		public void setMessage(String m) {
			message.setText(m);
		}

		private void reAdjust() {
			DialogLayout.Group vert = panel.createSequentialGroup(message, okb);
			DialogLayout.Group bg = panel.createSequentialGroup().addGap().addWidget(okb).addGap();
			DialogLayout.Group hor = panel.createParallelGroup().addWidget(message).addGroup(bg);
			panel.setVerticalGroup(vert);
			panel.setHorizontalGroup(hor);
		}

		@Override
		public Widget getWidget() {
			return panel;
		}
	}
	
	private static class Dialog extends DialogLayout{
		@Override
		public void layout() {
			super.layout();
			
			this.adjustSize();
			this.setPosition(MainCanvas.root.getInnerX() + (MainCanvas.root.getInnerWidth() - this.getWidth()) / 2, MainCanvas.root.getInnerY() + (MainCanvas.root.getInnerHeight() - this.getHeight()) / 2);
		}
	}

}
